package com.mask.im.business.controller.manage;

import com.mask.im.business.annotation.OptLog;
import com.mask.im.business.model.dto.RoleDTO;
import com.mask.im.business.model.dto.UserRoleDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.model.vo.RoleVO;
import com.mask.im.business.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.mask.im.business.common.constant.OptTypeConstant.DELETE;
import static com.mask.im.business.common.constant.OptTypeConstant.SAVE_OR_UPDATE;

/**
 * 角色模块
 *
 * @author jian
 */
@Api(tags = "角色模块")
@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "查询用户角色选项")
    @GetMapping("/admin/users/role")
    public RestResponse<List<UserRoleDTO>> listUserRoles() {
        return RestResponse.ok(sysRoleService.listUserRoles());
    }


    @ApiOperation(value = "查询角色列表")
    @GetMapping("/admin/roles")
    public RestResponse<PageResult<RoleDTO>> listRoles(ConditionVO conditionVO) {
        return RestResponse.ok(sysRoleService.listRoles(conditionVO));
    }

    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "保存或更新角色")
    @PostMapping("/admin/role")
    public RestResponse<?> saveOrUpdateRole(@RequestBody @Valid RoleVO roleVO) {
        sysRoleService.saveOrUpdateRole(roleVO);
        return RestResponse.ok();
    }

    @OptLog(optType = DELETE)
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/admin/roles")
    public RestResponse<?> deleteRoles(@RequestBody List<Integer> roleIdList) {
        sysRoleService.deleteRoles(roleIdList);
        return RestResponse.ok();
    }
}

